﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Drawing;

namespace BVVD_Website.ql_danh_muc
{
    public partial class frmDanhmuc2 : System.Web.UI.Page
    {
        static List<LST_DANHMUC> list;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                ddlLOAIDM.DataSource = (from loaidm in db.LST_LOAIDMs
                                        select loaidm).ToList();
                ddlLOAIDM.DataTextField = "TENLOAIDM";
                ddlLOAIDM.DataValueField = "MALOAIDM";
                ddlLOAIDM.DataBind();
                list = getData();
                grvDanhMuc.DataSource = list;
                if (list != null && list.Count > 0)
                {
                    lblMenu.Text = "Danh sách " + list[0].LST_LOAIDM.TENLOAIDM;
                }
                grvDanhMuc.DataBind();

            }
        }
        public List<LST_DANHMUC> getData()
        {
            BVVDDataStore<LST_DANHMUC> db = new BVVDDataStore<LST_DANHMUC>();
            Hashtable condition = new Hashtable();
            condition.Add("MALOAIDM", ddlLOAIDM.SelectedValue);
            return db.getFromDB(condition);
        }
        protected void grvDanhMuc_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvDanhMuc.DataSource = getData();

        }
        protected void grvDanhMuc_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            //BVVDDataStore<LST_LOAIDM> type = new BVVDDataStore<LST_LOAIDM>();
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                if (dataItem["TRANGTHAI"].Text == "0")
                    dataItem.ForeColor = Color.Silver;
            }
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                Label lblLOAIDM = editFormItem.FindControl("lblLOAIDM") as Label;

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;

                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    lblLOAIDM.Text = ddlLOAIDM.SelectedText;
                }
                else
                {
                    lblLOAIDM.Text = ddlLOAIDM.SelectedText;
                    rblTRANGTHAI.SelectedValue = editFormItem["TRANGTHAI"].Text;

                }
                ddlLOAIDM.DataBind();
            }
        }
        protected void grvDanhMuc_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            
            LST_DANHMUC danhmuc = new LST_DANHMUC
            {
                MALOAIDM = ddlLOAIDM.SelectedValue,
                MADM = (insertedItem.FindControl("txtMADM") as RadTextBox).Text,
                TENDM = (insertedItem.FindControl("txtTENDM") as RadTextBox).Text,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_DANHMUCs.InsertOnSubmit(danhmuc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới danh mục thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }


            rnAlert.Show();
        }
        protected void grvDanhMuc_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_DANHMUC danhmuc = (from item in db.LST_DANHMUCs
                                   where item.RECID.ToString().Equals(recid)
                                   select item).SingleOrDefault();
            //danhmuc.MALOAIDM = (editedItem.FindControl("ddlLOAIDM") as RadDropDownList).SelectedValue;
            danhmuc.MADM = (editedItem.FindControl("txtMADM") as RadTextBox).Text;
            danhmuc.TENDM = (editedItem.FindControl("txtTENDM") as RadTextBox).Text;
            danhmuc.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            danhmuc.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công danh mục có mã : " + danhmuc.MADM;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();

        }
        protected void grvDanhMuc_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_DANHMUC danhmuc = (from dm in db.LST_DANHMUCs
                                   where dm.RECID.ToString().Equals(recid)
                                   select dm).SingleOrDefault();
            db.LST_DANHMUCs.DeleteOnSubmit(danhmuc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Xóa thành công danh mục có mã : " + danhmuc.MADM;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }
        protected void ddlLOAIDM_SelectedIndexChanged(object sender, DropDownListEventArgs e)
        {
            BVVDDataStore<LST_DANHMUC> db = new BVVDDataStore<LST_DANHMUC>();
            Hashtable condition = new Hashtable();
            condition.Add("MALOAIDM", ddlLOAIDM.SelectedValue);
            lblMenu.Text = "Danh sách ".ToUpper() + ddlLOAIDM.SelectedText.ToUpper();

            grvDanhMuc.DataSource = db.getFromDB(condition);
            grvDanhMuc.DataBind();
        }
    }
}